<?PHP
/* 
date:2006.11.6
author:mailang

		# 总数
		$db = get_db_obj();
		$all_num = $db->loadresult('select count(*) from `#__user` where state=1 ');
		# 当前页
		$page = 1;
		if(isset($_GET['page'])) $page = intval($_GET['page']);
		# 对象
		$config=array(
			'list_num'=>$this->pagelist,
			'all_num'=>$all_num,
			'now_page'=>$page,
			'url'=>url($_REQUEST['m'],$_REQUEST['a']),
			'classname'=>'pagelist'
		);
		$page = new mlpage($config);
		$page_html = $page->get_page();
		# 得到详细数据
		$rs_list = $db->loadassoclist('select * from `#__aaa` where state=1  '.$page->get_limit());
 */
class mlpage{
	public static function & singleton()
	{
		static $instances;
		if (!isset ($instances))
		{
			$instances = new mlpage();
		}
		return $instances;
	}
	var $list_num = 0;
	var $all_num = 0;
	var $now_page = 0;
	var $url_array = array();
	var $classes = '';
	###
	var $all_page = 0;
	var $url = '';
	var $pagekey = 'page';
	var $limit = 0;
	function __construct(array $attr){
		 $this->list_num = intval($attr['list_num']);
		 $this->all_num = intval($attr['all_num']);
		 $this->now_page = intval($attr['now_page']);
		 $this->url = $attr['url'];
		 $this->classname = $attr['classname'];
		 $this->_init();
	}
	function _init(){
		$this->all_page = intval(ceil($this->all_num/$this->list_num));
		if($this->now_page <= 0) $this->now_page = 1;
		if($this->now_page >= $this->all_page) $this->now_page = $this->all_page;
		
		
		if($this->now_page <= 0)
			$this->limit = 0;
		else
			$this->limit = $this->list_num*($this->now_page - 1);		
		
	}
	function get_limit(){
		return ' limit '.$this->limit.','.$this->list_num.' ';
	}
	function _url($i){
		return $this->url.'&'.$this->pagekey.'='.intval($i);
	}
	function get_page(){
		$html = '<div class="'.$this->classname.'" >';
		if($this->now_page <= 1){
			$html .= '<span>前一页</span>';
		}else{
			$html .= '<span><a href="'.$this->_url($this->now_page - 1).'">前一页</a></span>';
		}
		$i=1;
		for($i;$i<=$this->all_page;$i++){
			if($this->now_page == $i){
				$html .= '<span>'.$i.'</span>';
			}else{
				$html .= '<span><a href="'.$this->_url($i).'">'.$i.'</a></span>';
			}
		}
		if($this->now_page == $this->all_page){
			$html .= '<span>后一页</span>';
		}else{
			$html .= '<span><a href="'.$this->_url($this->now_page + 1).'">后一页</a></span>';
		}
		$html .= '</div>';
		return $html;
	}
}
?>